package com.yantong.service.impl;

import com.yantong.utils.PageUtils;
import com.yantong.pojo.WorkClass;
import com.yantong.pojo.WorkClassExample;
import com.yantong.mapper.WorkClassMapper;
import com.yantong.service.WorkClassService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author kanghaixin
 * @date 2018/9/3.11:34
 */
@Service
public class WorkClassServiceImpl implements WorkClassService {

    @Autowired
    private WorkClassMapper workClassMapper;

    @Override
    public PageUtils<WorkClass> listOrderTypes(int offset, int limit) {
        PageUtils<WorkClass> pageUtils = new PageUtils<>();

        WorkClassExample workClassExample = new WorkClassExample();
        workClassExample.or().andIsdeleteEqualTo(0);

        int total = workClassMapper.countByExample(workClassExample);
        if (total >0) {
            pageUtils.setTotal(total);
            pageUtils.setRows(workClassMapper.selectByExample(workClassExample, new RowBounds(offset, limit)));
        }
        return pageUtils;
    }

    @Override
    public List<WorkClass> listAllTypes() {
        WorkClassExample workClassExample = new WorkClassExample();
        workClassExample.or().andIsdeleteEqualTo(0);
        return workClassMapper.selectByExample(workClassExample);
    }

    @Override
    public int save(WorkClass workClass) {
        if (workClass == null) {
            return 0;
        }
        return workClassMapper.insertSelective(workClass);
    }

    @Override
    public int update(WorkClass workClass) {
        if (workClass == null || workClass.getId() == null) {
            return 0;
        }
        return workClassMapper.updateByPrimaryKeySelective(workClass);
    }

    @Override
    public WorkClass getByKey(Long classId) {
        if (classId == null) {
            return null;
        }
        return workClassMapper.selectByPrimaryKey(classId);
    }
}
